﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CourierManager
{
    public class Route
    {
        private string pathToDatabase="../../Routes.txt";

        public string PathToDatabase
        {
            get { return pathToDatabase; }
            set { pathToDatabase = value; }
        }
        

        //enum of locations - Location
        private List<Location> locations;

        public List<Location> Locations
        {
            get { return locations; }
            set { locations = value; }
        }
        
        public string Name { get; set; }

        public List<string> AllRoutes { get; set; }

        //TODO: Decide which is better,parameterless constructor,or constructor containing with parameter List<Location>()
        public Route(string name, List<Location> locations)
        {
            this.Name = name;
            this.Locations = locations;
        }

        public Route()
            : this(null, null)
        { 
        }

        public void WriteInDatabase()
        {
            StreamWriter writeInFile = new StreamWriter(this.PathToDatabase,true);
            //this.AllRoutes = LoadAllRoutes();
            using (writeInFile)
            {
                writeInFile.Write(this.Name);
                foreach (Location location in this.Locations)
                {
                    writeInFile.Write("*{0}",location);
                }
                writeInFile.WriteLine();
            }
        }

        public List<string> LoadAllRoutes()
        {
            List<string> allRoutes = new List<string>();
            if (File.Exists(this.PathToDatabase))
            {
                StreamReader readFromFile = new StreamReader(this.PathToDatabase);
                string currentRow = readFromFile.ReadLine();
                using (readFromFile)
                {
                    while (currentRow!=null)
                    {
                        if (!String.IsNullOrEmpty(currentRow))
                        {
                            allRoutes.Add(currentRow);
                        }
                        currentRow = readFromFile.ReadLine();
                    }
                }
            }
            return allRoutes;
        }
    }
}
